package com.invernadero.invernaderotesis.Acceso_a_datos.AdminSqlite;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

public class AdminSqlite extends SQLiteOpenHelper{

	/***
	 * 
	 * * Constructor por defecto de la clase SQLite
	 * @param context
	 * @param name
	 * @param factory
	 * @param version
	 */
	public AdminSqlite(	Context context, String name,
									CursorFactory factory, int version
								)
	{
        super(context, name, factory, version);

	} // AdminSQLiteOpenHelper
	
	/***
	 * * Usado solo cuando se crea la BD la primera vez
	 * * despues se ejecutara solo el evento "onUpgrade"
	 * @param db: Representa la instancia de la BD
	 */
	@Override
	public void onCreate(SQLiteDatabase db)
	{
        try {
		String scriptCreacion = "CREATE TABLE [Persona] (" +
				" [idPersona] INTEGER NOT NULL primary key AUTOINCREMENT," +
				" [nombre] TEXT NOT NULL," +
				" [apellido] TEXT NOT NULL," +
				" [fechaNacimiento] TEXT NOT NULL," +
				" [correo] TEXT NOT NULL," +
				" [password] TEXT NOT NULL," +
				" [usuario] TEXT NOT NULL," +
				" [celular] TEXT NOT NULL" +
				//" Constraint [PK_Persona] PRIMARY KEY ([idPersona])"+
				" )";
		
		// Crea la Tabla Persona
		db.execSQL(scriptCreacion);
		
		scriptCreacion = "CREATE TABLE [Planta] (" +
				" [idPlanta] INTEGER NOT NULL primary key AUTOINCREMENT," +
				" [nombre] TEXT  NOT NULL," +
				" [descripcion] TEXT NULL" +
				//" Constraint [PK_Planta] PRIMARY KEY ([idPlanta]) AUTOINCREMENT"+
				" )";
		
		// Crea la Tabla Planta
		db.execSQL(scriptCreacion);
		
		scriptCreacion = "CREATE TABLE [Variable] (" +
				" [idVariable] INTEGER NOT NULL primary key AUTOINCREMENT," +
				" [nombre] TEXT  NOT NULL," +
				" [fecha] TEXT NULL," +
				" [medicion] TEXT NULL," +
				" [status] TEXT NULL" +
				//" Constraint [PK_Variable] PRIMARY KEY ([idVariable]) AUTOINCREMENT"+
				" )";
		
		// Crea la Tabla Variable
		db.execSQL(scriptCreacion);
		
		scriptCreacion = "CREATE TABLE [Tanque] (" +
				" [idTanque] INTEGER NOT NULL primary key AUTOINCREMENT," +
				" [cantidad] TEXT  NOT NULL," +
				" [tipo] TEXT NULL," +
				" [fecha] TEXT NULL," +
				" [status] TEXT NULL" +
				//" Constraint [PK_Tanque] PRIMARY KEY ([idTanque]) AUTOINCREMENT"+
				" )";
		
		// Crea la Tabla Tanque
		db.execSQL(scriptCreacion);
		
		scriptCreacion = "CREATE TABLE [Invernadero] (" +
				" [idInvernadero] INTEGER NOT NULL primary key AUTOINCREMENT," +
				" [nombre] TEXT NULL," +
				" [descripcion] TEXT NULL," +
				" [fkPersona] INTEGER NOT NULL," +
				//" Constraint [PK_Invernadero] PRIMARY KEY ([idInvernadero]) AUTOINCREMENT,"+
				" Constraint [FK_Invernadero_Persona] FOREIGN KEY ([fkPersona]) "+
				" References [Persona] ([idPersona])"+
				" )";
		
		// Crea la Tabla Invernadero
		db.execSQL(scriptCreacion);
		
		scriptCreacion = "CREATE TABLE [Timer] (" +
				" [idTimer] INTEGER NOT NULL primary key AUTOINCREMENT," +
				" [nombre] TEXT NULL," +
				" [fecha] TEXT NULL," +
				" [fkInvernadero] INTEGER NOT NULL," +
				//" Constraint [PK_Timer] PRIMARY KEY ([idTimer]) AUTOINCREMENT,"+
				" Constraint [FK_Timer_Invernadero] FOREIGN KEY ([fkInvernadero]) "+
				" References [Invernadero] ([idInvernadero])"+
				" )";
		
		// Crea la Tabla Timer
		db.execSQL(scriptCreacion);
		
		scriptCreacion = "CREATE TABLE [PlantaInver] (" +
				" [fkPlanta] INTEGER NOT NULL," +
				" [fkInvernadero] INTEGER NOT NULL," +
				" Constraint [PK_PlantaInver] PRIMARY KEY ([fkPlanta],[fkInvernadero]),"+
				" Constraint [FK_PI_Invernadero] FOREIGN KEY ([fkInvernadero]) "+
				" References [Invernadero] ([idInvernadero]),"+
				" Constraint [FK_PI_Planta] FOREIGN KEY ([fkPlanta]) "+
				" References [Planta] ([idPlanta])"+
				" )";
		
		// Crea la Tabla PlantaInver
		db.execSQL(scriptCreacion);
		
		scriptCreacion = "CREATE TABLE [VariableInver] (" +
				" [fkVariable] INTEGER NOT NULL," +
				" [fkInvernadero] INTEGER NOT NULL," +
				" Constraint [PK_VariableInver] PRIMARY KEY ([fkVariable],[fkInvernadero]),"+
				" Constraint [FK_VI_Invernadero] FOREIGN KEY ([fkInvernadero]) "+
				" References [Invernadero] ([idInvernadero]),"+
				" Constraint [FK_VI_Variable] FOREIGN KEY ([fkVariable]) "+
				" References [Variable] ([idVariable])"+
				" )";
		
		// Crea la Tabla VariableInver
		db.execSQL(scriptCreacion);
		
		scriptCreacion = "CREATE TABLE [TanqueInver] (" +
				" [fkTanque] INTEGER NOT NULL," +
				" [fkInvernadero] INTEGER NOT NULL," +
				" Constraint [PK_TanqueInver] PRIMARY KEY ([fkTanque],[fkInvernadero]),"+
				" Constraint [FK_TI_Invernadero] FOREIGN KEY ([fkInvernadero]) "+
				" References [Invernadero] ([idInvernadero]),"+
				" Constraint [FK_TI_Tanque] FOREIGN KEY ([fkTanque]) "+
				" References [Tanque] ([idTanque])"+
				" )";
		
		// Crea la Tabla VariableInver
		db.execSQL(scriptCreacion);

        }catch (IllegalArgumentException ex)
        {
            throw (new IllegalArgumentException("Error en el pase de los argumentos",ex));
        }
	}
	
	/***
	 * * Usado siempre que se indique que la version de la BD ha cambiado 
	 * * despues se ejecutara solo el evento "onUpgrade"
	 * @param db: Representa la instancia de la BD
	 * @param versionAnte: Indica la version que esta instalada actualmente
	 * @param versionNue: Indica la nueva version que esta inatalada
	 * *  Si versionAnte y versionNue son diferentes, se ejecuta este evento
	 * * Es importante que se eliminen los objetos si se quieren volver a crear
	 */
	@Override
	public void onUpgrade(SQLiteDatabase db, int versionAnte, int versionNue)
	{
        try {


        String scriptDrop = "DROP TABLE IF EXISTS [PlantaInver]";
		
		String scriptCreacion = "CREATE TABLE [PlantaInver] (" +
				" [fkPlanta] INTEGER NOT NULL," +
				" [fkInvernadero] INTEGER NOT NULL," +
				" Constraint [PK_PlantaInver] PRIMARY KEY ([fkPlanta],[fkInvernadero]),"+
				" Constraint [FK_PI_Invernadero] FOREIGN KEY ([fkInvernadero]) "+
				" References [Invernadero] ([idInvernadero]),"+
				" Constraint [FK_PI_Planta] FOREIGN KEY ([fkPlanta]) "+
				" References [Planta] ([idPlanta])"+
				" )";
		
		// Elimina la Tabla Planta
		db.execSQL(scriptDrop);	
		// Crea la Tabla PlantaInver
		db.execSQL(scriptCreacion);
		
		scriptDrop = "DROP TABLE IF EXISTS [VariableInver]";
		
		scriptCreacion = "CREATE TABLE [VariableInver] (" +
				" [fkVariable] INTEGER NOT NULL," +
				" [fkInvernadero] INTEGER NOT NULL," +
				" Constraint [PK_VariableInver] PRIMARY KEY ([fkVariable],[fkInvernadero]),"+
				" Constraint [FK_VI_Invernadero] FOREIGN KEY ([fkInvernadero]) "+
				" References [Invernadero] ([idInvernadero]),"+
				" Constraint [FK_VI_Variable] FOREIGN KEY ([fkVariable]) "+
				" References [Variable] ([idVariable])"+
				" )";
		
		// Elimina la Tabla VariableInver
		db.execSQL(scriptDrop);
		// Crea la Tabla VariableInver
		db.execSQL(scriptCreacion);
		
		scriptDrop = "DROP TABLE IF EXISTS [TanqueInver]";
		
		scriptCreacion = "CREATE TABLE [TanqueInver] (" +
				" [fkTanque] INTEGER NOT NULL," +
				" [fkInvernadero] INTEGER NOT NULL," +
				" Constraint [PK_TanqueInver] PRIMARY KEY ([fkTanque],[fkInvernadero]),"+
				" Constraint [FK_TI_Invernadero] FOREIGN KEY ([fkInvernadero]) "+
				" References [Invernadero] ([idInvernadero]),"+
				" Constraint [FK_TI_Tanque] FOREIGN KEY ([fkTanque]) "+
				" References [Tanque] ([idTanque])"+
				" )";
		
		// Elimina la Tabla TanqueInver
		db.execSQL(scriptDrop);
		// Crea la Tabla TanqueInver
		db.execSQL(scriptCreacion);
				
		scriptDrop = "DROP TABLE IF EXISTS [Persona]";
		
		scriptCreacion = "CREATE TABLE [Persona] (" +
				" [idPersona] INTEGER NOT NULL primary key AUTOINCREMENT," +
				" [nombre] TEXT NOT NULL," +
				" [apellido] TEXT NOT NULL," +
				" [fechaNacimiento] TEXT NOT NULL," +
				" [correo] TEXT NOT NULL," +
				" [password] TEXT NOT NULL," +
				" [usuario] TEXT NOT NULL," +
				" [celular] TEXT NOT NULL" +
				//" Constraint [PK_Persona] PRIMARY KEY ([idPersona]) AUTOINCREMENT"+
				" )";
		
		// Elimina la Tabla Persona
		db.execSQL(scriptDrop);
		// Crea la Tabla Persona
		db.execSQL(scriptCreacion);
		
	    scriptDrop = "DROP TABLE IF EXISTS [Planta]";
		
		scriptCreacion = "CREATE TABLE [Planta] (" +
				" [idPlanta] INTEGER NOT NULL primary key AUTOINCREMENT," +
				" [nombre] TEXT  NOT NULL," +
				" [descripcion] TEXT NULL," +
				//" Constraint [PK_Planta] PRIMARY KEY ([idPlanta]) AUTOINCREMENT"+
				" )";
		
		// Elimina la Tabla Planta
		db.execSQL(scriptDrop);
		// Crea la Tabla Planta
		db.execSQL(scriptCreacion);
		
		scriptDrop = "DROP TABLE IF EXISTS [Variable]";
		
		scriptCreacion = "CREATE TABLE [Variable] (" +
				" [idVariable] INTEGER NOT NULL primary key AUTOINCREMENT," +
				" [nombre] TEXT  NOT NULL," +
				" [fecha] TEXT NULL," +
				" [medicion] TEXT NULL," +
				" [status] TEXT NULL" +
				//" Constraint [PK_Variable] PRIMARY KEY ([idVariable]) AUTOINCREMENT"+
				" )";
		
		// Elimina la Tabla Persona
		db.execSQL(scriptDrop);
		// Crea la Tabla Variable
		db.execSQL(scriptCreacion);
		
		scriptDrop = "DROP TABLE IF EXISTS [Tanque]";
		
		scriptCreacion = "CREATE TABLE [Tanque] (" +
				" [idTanque] INTEGER NOT NULL primary key AUTOINCREMENT," +
				" [cantidad] TEXT  NOT NULL," +
				" [tipo] TEXT NULL," +
				" [fecha] TEXT NULL," +
				" [status] TEXT NULL" +
				//" Constraint [PK_Tanque] PRIMARY KEY ([idTanque]) AUTOINCREMENT"+
				" )";
		
		// Elimina la Tabla Tanque
		db.execSQL(scriptDrop);
		// Crea la Tabla Tanque
		db.execSQL(scriptCreacion);
		
		scriptDrop = "DROP TABLE IF EXISTS [Invernadero]";
		
		scriptCreacion = "CREATE TABLE [Invernadero] (" +
				" [idInvernadero] INTEGER NOT NULL primary key AUTOINCREMENT," +
				" [nombre] TEXT NULL," +
				" [descripcion] TEXT NULL," +
				" [fkPersona] INTEGER NOT NULL," +
				//" Constraint [PK_Invernadero] PRIMARY KEY ([idInvernadero]) AUTOINCREMENT,"+
				" Constraint [FK_Invernadero_Persona] FOREIGN KEY ([fkPersona]) "+
				" References [Persona] ([idPersona])"+
				" )";
		
		// Elimina la Tabla Invernadero
		db.execSQL(scriptDrop);
		// Crea la Tabla Invernadero
		db.execSQL(scriptCreacion);
		
		scriptDrop = "DROP TABLE IF EXISTS [Timer]";
		
		scriptCreacion = "CREATE TABLE [Timer] (" +
				" [idTimer] INTEGER NOT NULL primary key AUTOINCREMENT," +
				" [nombre] TEXT NULL," +
				" [fecha] TEXT NULL," +
				" [fkInvernadero] INTEGER NOT NULL," +
				//" Constraint [PK_Timer] PRIMARY KEY ([idTimer]) AUTOINCREMENT,"+
				" Constraint [FK_Timer_Invernadero] FOREIGN KEY ([fkInvernadero]) "+
				" References [Invernadero] ([idInvernadero])"+
				" )";
		
		// Elimina la Tabla Invernadero
		db.execSQL(scriptDrop);
		// Crea la Tabla Invernadero
		db.execSQL(scriptCreacion);

        }catch (IllegalArgumentException ex)
        {
            throw (new IllegalArgumentException("Error en el pase de los argumentos",ex));
        }
	}
}
